package v1

import (
	"copper-cloud/models"
	"copper-cloud/pkg/app"
	"copper-cloud/pkg/e"
	"copper-cloud/pkg/util"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

func GetApps(c *gin.Context) {
	traceId := util.GetTraceID(c)
	appG := app.Gin{C: c}

	apps, err := models.AppSelects()
	if err != nil {

		appG.Response(http.StatusInternalServerError, e.ERROR, traceId, nil)
		return
	}

	appG.Response(http.StatusOK, e.SUCCESS, traceId, apps)
}

func GetApp(c *gin.Context) {
	traceId := util.GetTraceID(c)
	appG := app.Gin{C: c}

	appId, paramErr := strconv.Atoi(c.Param("app_id"))

	if paramErr != nil {

		appG.Response(http.StatusBadRequest, e.ErrorDeptIdMustBeInt, traceId, nil)
		return
	}

	appInfo, err := models.GetAppInfoById(appId)
	if err != nil {

		appG.Response(http.StatusInternalServerError, e.ERROR, traceId, nil)
		return
	}

	appG.Response(http.StatusOK, e.SUCCESS, traceId, appInfo)
}

func GetAppEnvs(c *gin.Context) {
	traceId := util.GetTraceID(c)
	appG := app.Gin{C: c}

	appId, paramErr := strconv.Atoi(c.Param("app_id"))

	if paramErr != nil {

		appG.Response(http.StatusBadRequest, e.ErrorDeptIdMustBeInt, traceId, nil)
		return
	}

	appEnvs, err := models.AppEnvSelects(appId)
	if err != nil {
		appG.Response(http.StatusInternalServerError, e.ERROR, traceId, nil)
		return
	}

	var namespaces []string
	for _, v := range appEnvs {
		namespaces = append(namespaces, v.AppNamespace)
	}

	appG.Response(http.StatusOK, e.SUCCESS, traceId, namespaces)
}

type MysqlInfo struct {
	Host string `json:"host"`
	Port int `json:"port"`
	DB string `json:"db"`
	User string `json:"user"`
	Password string `json:"password"`
}

type RedisInfo struct {
	Host string `json:"host"`
	Port int `json:"port"`
	DB int `json:"db"`
	Password string `json:"password"`
}

type LinuxInfo struct {
	Host string `json:"host"`
	Port int `json:"port"`
	User string `json:"user"`
	Password string `json:"password"`
}

type ZookeeperInfo struct {
	Host string `json:"host"`
	Port int `json:"port"`
	Password string `json:"password"`
}

func GetAppEnv(c *gin.Context) {
	traceId := util.GetTraceID(c)
	appG := app.Gin{C: c}

	appId, paramErr := strconv.Atoi(c.Param("app_id"))

	if paramErr != nil {

		appG.Response(http.StatusBadRequest, e.ErrorDeptIdMustBeInt, traceId, nil)
		return
	}

	appNamespace := c.Param("app_namespace")

	appEnv, err := models.GetAppEnvById(appId, appNamespace)
	if err != nil {
		appG.Response(http.StatusInternalServerError, e.ERROR, traceId, nil)
		return
	}

	if appEnv == nil {
		appG.Response(http.StatusOK, e.SUCCESS, traceId, nil)
		return
	}

	data := make(map[string]interface{})

	mysqlInfo := MysqlInfo{
		Host:     appEnv.AppMysqlHost,
		Port:     appEnv.AppMysqlPort,
		DB:       appEnv.AppMysqlDB,
		User:     appEnv.AppMysqlUser,
		Password: appEnv.AppMysqlPassword,
	}

	redisInfo := RedisInfo{
		Host:     appEnv.AppRedisHost,
		Port:     appEnv.AppRedisPort,
		DB:       appEnv.AppRedisDB,
		Password: appEnv.AppRedisPassword,
	}

	linuxInfo := LinuxInfo{
		Host:     appEnv.AppLinuxHost,
		Port:     appEnv.AppLinuxPort,
		User:     appEnv.AppLinuxUser,
		Password: appEnv.AppLinuxPassword,
	}

	zookeeperInfo := ZookeeperInfo{
		Host:     appEnv.AppZookeeperHost,
		Port:     appEnv.AppZookeeperPort,
		Password: appEnv.AppZookeeperPassword,
	}

	data["mysql"] = mysqlInfo
	data["redis"] = redisInfo
	data["linux"] = linuxInfo
	data["zookeeper"] = zookeeperInfo

	appG.Response(http.StatusOK, e.SUCCESS, traceId, data)
}
